<?php
class blog extends spController
{

	function getBlogByLink() {
		$c = new SaeTClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);

		$mblogUrl = explode('/', 'http://weibo.com/1056087203/eBevRBJxM6X');
		$mid      = end($mblogUrl);
		$apiUrl	  = 'http://api.t.sina.com.cn/queryid.json?mid='.$mid.'&isBase62=1&type=1';
		$ret = json_decode(file_get_contents($apiUrl));
		$mbid = $ret->id;

		$ret  = $c->show_status($mbid);
		echo '<img src="'.$ret['user']['profile_image_url'].'">'.$ret['user']['name'].':'.$ret['text'];
	}

	function needDistribute() {
		$mbid = $this->spArgs('mbid', '11437824654');
		$blogUrl = $this->spArgs('blogurl', 'http://weibo.com/1056087203/eBevRBJxM6X');
		$uid  = $_SESSION['uid'];
		$bonus= $this->spArgs('bonus', '500');
		$bonusTotal = $this->spArgs('bonus_total', '6000');
		$followersLimit = $this->spArgs('followers_limit', '100');

		$mUser = spClass('user');
		$me    = $mUser->getMe();
		if ($bonusTotal > $me['cash']) {
			$this->error('Cash not enouch', spUrl('blog', 'getBlogByLink'));
		}
		$mDistribute = spClass('distribute_task');
		$data = array(
					'mbid'	=> $mbid,
					'uid'	=> $uid,
					'blogurl' => $blogUrl,
					'bonus'	  => $bonus,
					'bonus_total'	 => $bonusTotal,
					'followers_limit'=> $followersLimit,
					'dateline'	     => time()
					);
		$msg = $mDistribute->create($data);dump($msg);
		if (!$msg['error']) {
			$c = new SaeTClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			$blogContent = "I have published a blog to you . Click here for have a look. http://".$_SERVER['HTTP_HOST']."/index.php?c=main&a=home";
			$c->update($blogContent);
			$this->success('发布转发任务成功', spUrl('main', 'home'));
		} else {
			$this->error($msg['error'], spUrl('blog', 'getBlogByLink'));
		}
	}

	function distribute() {
		$mbid = $this->spArgs('mbid', '11437824654');
		$text = $this->spArgs('text', '转发测试');
		$isComment = $this->spArgs('is_comment');

		$c = new SaeTClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		$msg = $c->repost($mbid, $text);
		if ($msg['id']) {
			$mDistribute = spClass('distribute_task');
			$row   = $mDistribute->find(array('mbid'=>$mbid));
			$bonus = $row['bonus'];
			
			$mDistribution = spClass('distribution');
			$data = array(
						'uid' => $_SESSION['uid'],
						'mbid'=> $msg['id'],
						'originid' => $mbid,
						'dateline' => time()
						);
			$mDistribution->create($data);

			$mUser = spClass('user');
			$this->success('转发成功，您已获得'.$bonus.'微币', spUrl('main', 'home'));
		} else {dump($msg);
			$this->error($msg['error'], spUrl('blog', 'getBlogByLink'));
		}
	}

}